package com.cyan.note.controller.manager;

import com.cyan.note.common.ApiResult;
import com.cyan.note.common.PageResult;
import com.cyan.note.dto.manager.customer.CustomerListDTO;
import com.cyan.note.dto.manager.customer.CustomerValidationDTO;
import com.cyan.note.entity.Customer;
import com.cyan.note.service.manager.ManagerCustomerService;
import com.cyan.note.util.SecurityUtils;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 店长端-客户管理控制器
 * 
 * @author ZhangzhenYu
 * @since 2025-01-15
 */
@RestController
@RequestMapping("/api/manager/customers")
public class ManagerCustomerController {

    @Autowired
    private ManagerCustomerService managerCustomerService;

    /**
     * 分页查询客户列表
     */
    @GetMapping("/list")
    public ApiResult<PageResult<CustomerListDTO>> getCustomerList(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "20") Integer size,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String status,
            @RequestParam(required = false) String level,
            @RequestParam(required = false) String source,
            @RequestParam(required = false) String registerStart,
            @RequestParam(required = false) String registerEnd,
            @RequestParam(required = false) String minAmount,
            @RequestParam(required = false) String maxAmount,
            @RequestParam(required = false) String minOrders,
            @RequestParam(required = false) String maxOrders,
            @RequestParam(defaultValue = "id") String sortBy,
            @RequestParam(defaultValue = "desc") String sortOrder
    ) {
        Long shopId = SecurityUtils.getCurrentShopId();
        return managerCustomerService.getCustomerList(shopId, page, size, keyword, status, 
                level, source, registerStart, registerEnd, minAmount, maxAmount, 
                minOrders, maxOrders, sortBy, sortOrder);
    }

    /**
     * 查询客户详情
     */
    @GetMapping("/{id}")
    public ApiResult<Customer> getCustomerDetail(@PathVariable Long id) {
        Long shopId = SecurityUtils.getCurrentShopId();
        return managerCustomerService.getCustomerDetail(shopId, id);
    }

    /**
     * 新增客户
     */
    @PostMapping
    public ApiResult<Void> createCustomer(@Valid @RequestBody Customer customer) {
        Long shopId = SecurityUtils.getCurrentShopId();
        Long operatorId = SecurityUtils.getCurrentUserId();
        customer.setShopId(shopId);
        return managerCustomerService.createCustomer(customer, operatorId);
    }

    /**
     * 更新客户信息
     */
    @PutMapping("/{id}")
    public ApiResult<Void> updateCustomer(@PathVariable Long id, @Valid @RequestBody Customer customer) {
        Long shopId = SecurityUtils.getCurrentShopId();
        Long operatorId = SecurityUtils.getCurrentUserId();
        customer.setId(id);
        customer.setShopId(shopId);
        return managerCustomerService.updateCustomer(customer, operatorId);
    }

    /**
     * 删除客户
     */
    @DeleteMapping("/{id}")
    public ApiResult<Void> deleteCustomer(@PathVariable Long id) {
        Long shopId = SecurityUtils.getCurrentShopId();
        Long operatorId = SecurityUtils.getCurrentUserId();
        return managerCustomerService.deleteCustomer(shopId, id, operatorId);
    }

    /**
     * 客户校验（用于手动下单时的客户搜索）
     */
    @GetMapping("/validate")
    public ApiResult<CustomerValidationDTO> validateCustomer(@RequestParam String nickname) {
        Long shopId = SecurityUtils.getCurrentShopId();
        return managerCustomerService.validateCustomer(shopId, nickname);
    }

    /**
     * 更新客户状态
     */
    @PutMapping("/{id}/status")
    public ApiResult<Void> updateCustomerStatus(
            @PathVariable Long id,
            @RequestParam String status
    ) {
        Long shopId = SecurityUtils.getCurrentShopId();
        Long operatorId = SecurityUtils.getCurrentUserId();
        return managerCustomerService.updateCustomerStatus(shopId, id, status, operatorId);
    }

    /**
     * 更新客户等级
     */
    @PutMapping("/{id}/level")
    public ApiResult<Void> updateCustomerLevel(
            @PathVariable Long id,
            @RequestParam String level
    ) {
        Long shopId = SecurityUtils.getCurrentShopId();
        Long operatorId = SecurityUtils.getCurrentUserId();
        return managerCustomerService.updateCustomerLevel(shopId, id, level, operatorId);
    }
}